package com.qriver.distributedlock.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CuratorConfig {

    @Value("${zk.connectString:127.0.0.1:2181}")
    private String connectString;

    /**
     * 注入CuratorFramework对象，用来创建分布式锁，其中start是初始化方法，close是注销方法
     * @return
     */
    @Bean(initMethod="start",destroyMethod = "close")
    public CuratorFramework getCuratorFramework() {
        //设置重试规则
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        //Zookeeper客户端
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        return client;
    }

}
